/**
 * @作者：耿
 * @时间：2022-11-05
 */

import { observable, configure, action, runInAction } from 'mobx'
import axios from 'axios'

configure({
    enforceActions: 'always'
})


//面向对象方式初始化 （class 方式）
class Store {
    @observable is = true  //@ ES7 装饰器语法， java中是注解 
    @observable name = 'geng'
    @observable list = []
    @observable totalNum = 0
    @observable totalPrice = 0
    @observable cartProd = [{
        name: '大米',
        price: 20,
        num: 0
    }, {
        name: '大米2',
        price: 20,
        num: 0
    }]

    @action changeTotalNum() {
        this.totalNum++
    }

    @action addCart(obj) {
        this.changeTotalNum()
        console.log('obj', obj)
        this.cartProd.push(obj)
        console.log('this.cartProd', this.cartProd)
        
    }

    @action getList() {
        let _this = this;
        axios.get('http://localhost:3000/users').then(
            res => {
                if (res.status === 200) {
                    runInAction(() => {
                        // _this.list = res.data
                        _this.list = [{
                            id: 1,
                            name: 'geng'
                        }]

                    })
                }
            },
            err => {
                console.log('err', err)
            }
        )
    }

}

//初始化对象
const userStore = new Store()

export default userStore